package top.huhuiyu.springboot.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.huhuiyu.springboot.study.base.BaseDataResult;
import top.huhuiyu.springboot.study.base.BaseResult;
import top.huhuiyu.springboot.study.base.PageBean;
import top.huhuiyu.springboot.study.base.PageResult;
import top.huhuiyu.springboot.study.entity.FootballClub;
import top.huhuiyu.springboot.study.mapper.FootballClubMapper;
import top.huhuiyu.springboot.study.service.FootballClubService;

import java.util.List;

/**
 * 足球俱乐部服务实现类
 *
 * @author 胡辉煜
 */
@Service
// 可以自动将final的字段生成构造函数，也就可以完成spring推荐的构造函数注入
@RequiredArgsConstructor
public class FootballClubServiceImpl implements FootballClubService {
  private final FootballClubMapper footballClubMapper;

  @Override
  public BaseResult add(FootballClub footballClub) {
    int i = footballClubMapper.add(footballClub);
    return i == 1 ? BaseResult.ok("添加成功") : BaseResult.fail("添加失败");
  }

  @Override
  public BaseDataResult<FootballClub> queryByKey(Integer fcid) {
    FootballClub club = footballClubMapper.queryByKey(fcid);
    if (club == null) {
      return BaseDataResult.fail("没有查询到信息", FootballClub.class);
    } else {
      return BaseDataResult.ok(club);
    }
  }

  @Override
  public BaseDataResult<PageResult<List<FootballClub>>> query(FootballClub club, PageBean pageBean, Integer order) {
    PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
    Page<FootballClub> page = (Page<FootballClub>) footballClubMapper.query(club, order);
    pageBean.setTotal(page.getTotal());
    return BaseDataResult.ok(PageResult.instance(pageBean, page.getResult()));
  }


  @Override
  public BaseResult update(FootballClub footballClub) {
    int i = footballClubMapper.update(footballClub);
    return i == 1 ? BaseResult.ok("修改成功") : BaseResult.fail("修改失败");
  }

  @Override
  public BaseResult delete(Integer fcid) {
    int i = footballClubMapper.delete(fcid);
    return i == 1 ? BaseResult.ok("删除成功") : BaseResult.fail("删除失败");
  }

}
